<?php

/**
 * 挖宝用户 Service
 * 
 * Apps/Common/Service/WxEventTreasureUserService.class.php
 * Zix @ 2015-08-26
 */

namespace Common\Service;

class WxEventTreasureUserService {

  private static $instance;

  public static function instance() {
    if (self::$instance == null) {
      $instance = new WxEventTreasureUserService();
    }

    return $instance;
  }

  function get_default_row() {
    return array(
      'id' => '',
      'event_type' => '',
      'event_id' => '',
      'user_id' => '',
      'user_nickname' => '',
      'user_phone' => '',
      'point' => '',
      'award_id' => '',
      'award_sn' => '',
      'assist_num' => '0',
      'ipaddress' => '',
      'create_time' => date('Y-m-d H:i:s'),
    );
  }

  function get_by_id($id) {
    $WxEventTreasureUser = M('WxEventTreasureUser');
    $data = $WxEventTreasureUser->find($id);
    return $data ? $data : array();
  }

  function get_by_cond($config) {
    $default = array(
      'page' => 1,
      'page_size' => 12,
      'status' => '',
      'event_type' => '',
      'event_id' => '',
      'user_id' => '',
      'count' => FALSE,
      'order' => 'DESC',
      'sort' => 'id',
    );

    $config = extend($config, $default);

    $WxEventTreasureUser = M('WxEventTreasureUser');

    $where = array();

    if (!empty($config['status'])) {
      $where['etu.status'] = $config['status'];
    }
    
    if (!empty($config['event_id'])) {
      $where['etu.event_id'] = $config['event_id'];
    }
    
    if (!empty($config['event_type'])) {
      $where['etu.event_type'] = $config['event_type'];
    }
    
    if (!empty($config['user_id'])) {
      $where['etu.user_id'] = $config['user_id'];
    }
    if ($config['count']) {
      return $WxEventTreasureUser->where($where)->count();
    } else {
      $order = 'etu.' . $config['sort'] . ' ' . $config['order'];
      $limit = ($config['page'] - 1 ) * $config['page_size'] . ' , ' . $config['page_size'];
      $data = $WxEventTreasureUser
        ->field('etu.* , u.nickname as user_nickname , u.phone as user_phone')
        ->alias('etu')
        ->join('mer_user u on u.id = etu.user_id' , 'left')
        ->where($where)
        ->limit($limit)->order($order)->select();
      //echo $WxEventTreasureUser->_sql();
      return $data ? $data : array();
    }
  }

  function create($data, $is_ajax = true) {
    $WxEventTreasureUser = M('WxEventTreasureUser');
    $ret = $WxEventTreasureUser->add($data);
    if ($is_ajax) {
      if ($ret) {
        return ajax_arr('添加成功', TRUE, array(
          'id' => $ret
        ));
      } else {
        return ajax_arr('添加失败', FALSE);
      }
    } else {
      if ($ret) {
        return $ret;
      } else {
        return 0;
      }
    }
  }

  function update($id, $data) {
    $WxEventTreasureUser = M('WxEventTreasureUser');

    $ret = $WxEventTreasureUser->where("id = %d", $id)->save($data);
    if ($ret) {
      return ajax_arr('编辑成功', TRUE);
    } else {
      return ajax_arr('编辑失败', FALSE);
    }
  }

  function delete($ids) {
    $WxEventTreasureUser = M('WxEventTreasureUser');
    $ret = $WxEventTreasureUser->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }

  function get_by_uid( $user_id , $event_id , $event_type = 'Treasure' ) {
    //取活动
    $WxEventTreasure = M('WxEventTreasure');
    $event_data = $WxEventTreasure->where('id = %d' , $event_id )->find();
    
    if ( empty( $event_data ) ) {
      return array();
    }
    
    //取是否参与活动
    $data = $this->_get_by_uid($user_id, $event_id);
    
    //如果没有参与
    if( empty($data) ) {
      //创建活动
      $add_data = array(
        'event_type' => 'Treasure' ,
        'event_id' => $event_id ,
        'user_id' => $user_id ,
        'point' => mt_rand($event_data['init_min_point'], $event_data['init_max_point']) ,
        'ipaddress' => get_client_ip(0, TRUE )
      );
      $this->create( $add_data );
      $data = $this->_get_by_uid($user_id, $event_id);
    }
    return $data ? $data : array() ;
  }
  
  function _get_by_uid($user_id , $event_id) {
    $WxEventTreasureUser = M('WxEventTreasureUser');
    $where = array(
      'etu.user_id'  => $user_id ,
      'etu.event_id' => $event_id ,
    );
    
    $data = $WxEventTreasureUser
      ->alias('etu')
      ->field('etu.* , u.nickname as user_nickname, u.phone as user_phone , u.icon as header_icon')
      ->where($where)
      ->join('mer_user u on u.id = etu.user_id' , 'left')
      ->find();
    return $data ? $data : array() ;
  }
  
  /**
   * 
   * @param type $event_id
   * @param type $user_id
   * @param type $data
   * @return type
   */
  function take_award( $event_id , $user_id , $data ) {    

  }
}
